﻿package 
{
	import flash.display.Sprite;
	import flash.events.*;
	import org.summerTree.graphics.TaiJi;
	import org.summerTree.graphics.GearWheel;

	public class Main extends Sprite
	{
		private var list:Array=[];
		private var pen:Sprite=new Sprite();
		private var num:int=20;
		public function Main()
		{
			init();
		}
		private function init():void
		{
			addChild(pen);
			pen.graphics.lineStyle(1);

			//创建太极图和齿轮图
			for (var i:int=0; i<num; i++)
			{
				if (i%2==0)
				{
					var taiji:TaiJi=new TaiJi(25);
					taiji.x=50+taiji.width*i;
					taiji.y=50;
					addChild(taiji);
					list.push(taiji);
				}
				else
				{
					var gear:GearWheel=new GearWheel();
					gear.CreatGearWheel(25,20,15);
					addChild(gear);
					list.push(gear);
				}
			}
			addEventListener(Event.ENTER_FRAME,Run);
		}
		private function Run(event:Event):void
		{
			pen.graphics.clear();
			for (var i:int=1; i<num; i++)
			{
				var dx:Number=list[i-1].x-list[i].x;
				var dy:Number=list[i-1].y-list[i].y;
				var angle:Number=Math.atan2(dy,dx);
				list[i].rotation=angle*180/Math.PI+90;

				//采用了缓冲的公式
				list[i].x=list[i].x+dx/10;
				list[i].y=list[i].y+dy/10;
			}
			var dx2:Number=mouseX-list[0].x;
			var dy2:Number=mouseY-list[0].y;
			var angle2:Number=Math.atan2(dy2,dx2);
			list[0].rotation=angle2*180/Math.PI+90;
			list[0].x+=(mouseX-list[0].x)/10;
			list[0].y+=(mouseY-list[0].y)/10;

			//绘线
			pen.graphics.lineStyle(1);
			pen.graphics.moveTo(mouseX,mouseY);
			pen.graphics.lineTo(list[0].x,list[0].y);

		}
	}

}